package com.ebelter.btcomlib.utils;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.widget.TextView;

import com.ebelter.btcomlib.CommonLib;
import com.ebelter.btcomlib.utils.log.LogUtils;


public class StringUtils {
    private static final String NON = "";

    public static String getResStr(int strRes){
        return CommonLib.APPContext.getString(strRes);
    }

    public static boolean isEmpty(String string){
        return string == null || string.length() == 0;
    }


    //截取字符串
    public static String subString(String timeStr,int form,int to){
        if (isEmpty(timeStr)||to<form||timeStr.length()<to){
            return NON;
        }

        return timeStr.substring(form,to);
    }
    //截取字符串
    public static String subString(String timeStr,int form){
        if (isEmpty(timeStr)||timeStr.length()<form){
            return NON;
        }

        return timeStr.substring(form);
    }

    public static void setTextUseSpannable(TextView textView, int value, String unit, int textSize){



        try {
            if (textView==null){
                return;
            }
            if (value<=0){
                textView.setText("--");
            }else {
                String local = AppUtils.getLocalStr();
                String result;
                if (TextUtils.equals(local,AppUtils.iw)){
                    result =  unit+value;

                    SpannableString spannableString =getSpannableString(result,textSize,0,unit.length());
                    textView.setText(spannableString);

                }else {
                    //不需要取反的
                    result = value+unit;


                    if (TextUtils.equals(local,AppUtils.ru)){
                        SpannableString spannableString =getSpannableString(result,textSize,(value+"").length(),result.length());
                        textView.setText(spannableString);
                    }else {
                        textView.setText(result);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }



    }
    public static SpannableString getSpannableString(String content,int textSize,int start,int end){
        SpannableString spanString = new SpannableString(content);
        AbsoluteSizeSpan span = new AbsoluteSizeSpan(textSize);
        spanString.setSpan(span, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return spanString;
    }
}
